import request from '@/utils/request';
import {download, toFormData, checkDownloadRes} from '@/utils/common';

/**
 * 分页查询分类信息
 */
export async function pageTypes(params) {
  const res = await request.get('/admin/cms/nav/type/list', {params});
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}
/**
 * 查询分类下拉列表
 * @returns {Promise<any>}
 */
export async function dropDownListTypes() {
  const res = await request.get('/admin/cms/type/dropDownList');
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加菜单类别
 */
export async function addType(data) {
  const res = await request.post('/admin/cms/nav/type', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改菜单类别
 */
export async function updateType(data) {
  const res = await request.put('/admin/cms/nav/type', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 删除菜单类别
 */
export async function removeType(id) {
  const res = await request.delete('/admin/cms/nav/type/' + id);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除菜单类别
 */
export async function removeTypes(ids) {
  const res = await request.delete('/admin/cms/nav/type/' + ids.join());
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改类别状态
 */
export async function updateTypesStatus(id, status) {
  const res = await request.put('/admin/cms/nav/type/changeStatus', {
    id,
    status
  });
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导出分类信息
 */
// export async function exportTypes(params) {
//   const res = await request({
//     url: '/admin/cms/type/export',
//     method: 'POST',
//     data: toFormData(params),
//     responseType: 'blob'
//   });
//   await checkDownloadRes(res);
//   download(res.data, `type_${Date.now()}.xlsx`);
// }
